package ludo;

import java.io.Serializable;

//represents a player's pieces
public class Player implements Serializable{
	static final long serialVersionUID = 674532476;

	public final int boardSize = 13*3+19;
	final int delta = 13;
	// Player's pieces on board
	public int[] board;
	// Player id, 0-3
	int id;
	
	Player(int id){
		// Initialize player and player board
		this.id=id;
		board = new int[boardSize];
		for(int i=0;i<boardSize;i++){
			board[i]=0;
		}
		board[0]=4;
	}
	
	public boolean equals(Player other){
		if (id != other.id) return false;
		if(!java.util.Arrays.equals(this.board, other.board)) return false;
		return true;
		
	}
	
	// Convert from game index to player relative index
	public int index(int i){
		int delta=13;
		if (i>=4 && i<=55){
			int j=i-3-delta*id;
			if(j<0){
				j+=52;
			}
			return j;
		} else if (i<=3){
			// Other player's start square
			if (i>id) {
				return -1;
			}
			return i-id; 
		} else if (i>=51){
			return i-4-id*6;
		}
		return 0;
	}
	

	
	// Return number of pieces on square i
	public int pieces(int i){
		i=index(i);
		if ( i<0 || i>boardSize-1){
			return -1;
		}
		return board[i];
	}
	
	// Move one piece on square i n steps forward
	// TODO: Check for out of bounds
	public void move(int i, int n)  {
		board[index(i)]--;
		board[index(i)+n]++;
	}
	
	// Return no. pieces in start square
	public int getStart(){
		return board[0];
	}

	// Return no. pieces in finish square
	int getFinish(){
		return board[boardSize];
	}
	
	// Move player piece to start square
	void home(int i){
		board[index(i)]--;
		board[0]++;
		
	}
}
